﻿using AutoMapper;
using Blog.Application.Contract.Blogs;
using Blog.Application.Contract.Blogs.Dto;
using Blog.EntityFrameworkCore;
using Infrastructure;
using Microsoft.EntityFrameworkCore;

namespace Blog.Application.Blogs
{
    /// <summary>
    /// 博客
    /// </summary>
    public class BlogServer : IBlogServer
    {
        private readonly BlogDbContext _blogDbContext;
        private readonly IMapper _mapper;
        private readonly CurrentService _currentService;

        public BlogServer(BlogDbContext blogDbContext, IMapper mapper, CurrentService currentService)
        {
            _blogDbContext = blogDbContext;
            _mapper = mapper;
            _currentService = currentService;
        }

        /// <summary>
        /// 创建博客文章
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="BusinessExecptions"></exception>
        public async Task CreateBlogAsync(CreateBlogsDto input)
        {
            var userId = _currentService.GetUserId();
            if (await _blogDbContext.BlogTypes.AnyAsync(x => x.Id == input.TypeId))
            {
                throw new BusinessExecptions("博客文章类型不存在");
            }

            var data = _mapper.Map<Module.Blogs>(input);
            data.AuthorId = userId;

            await _blogDbContext.AddAsync(data);
            await _blogDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 删除博客
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>       
        public async Task DeleteAsync(Guid id)
        {
            var userId = _currentService.GetUserId();
            var data = await _blogDbContext.Blogs.FirstOrDefaultAsync(x => x.Id == id && x.AuthorId == userId);
            if (data != null)
            {
                _blogDbContext.Blogs.Remove(data);
                await _blogDbContext.SaveChangesAsync();
            }

        }

        /// <summary>
        /// 查询博客文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<BlogDto> GetAsync(Guid id)
        {
            var data = await _blogDbContext.Blogs.Where(x => x.Id == id)
                .AsSplitQuery()     //拆分查询
                .Include(x => x.BlogType)   //导航属性查询类型信息
                .Include(x => x.Author)     //导航属性用户查询
                .FirstOrDefaultAsync();

            if (data == null)
            {
                throw new BusinessExecptions("博客文章不存在");
            }
            var dto = _mapper.Map<BlogDto>(data);
            return dto;
        }

        /// <summary>
        /// 编辑博客
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateAsync(CreateBlogsDto input)
        {
            var useId = _currentService.GetUserId();
            var data = await _blogDbContext.Blogs.FirstOrDefaultAsync(x => x.AuthorId == useId);

            if (data == null)
            {
                throw new BusinessExecptions("博客文章不存在");
            }

            // 将input数据映射到data
            _mapper.Map(input, data);

            _blogDbContext.Blogs.Update(data);
            await _blogDbContext.SaveChangesAsync();
        }
    }
}
